﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;
using System.Text;

using XMK.Tadong.Biz;

namespace XMK.Tadong.Web.Language
{
    public class FileExplorerPage : System.Web.UI.Page
    {
        protected FileExplorerConfig config;
        protected LanguageDictionary lang;

        private static List<string> _keywordList;
        public static List<string> KeywordList
        {
            get
            {
                if (_keywordList != null) return _keywordList;
                _keywordList = new List<string>();
                // 初始化关键词字典
                string keywordFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "webdata\\searchDict.txt");
                if (!File.Exists(keywordFilePath)) return _keywordList;
                string[] keywordArr = File.ReadAllLines(keywordFilePath, Encoding.UTF8);
                foreach (var keyword in keywordArr)
                {
                    if (string.IsNullOrEmpty(keyword) || keyword.Trim().StartsWith("#")) continue;
                    _keywordList.Add(keyword);
                }
                return _keywordList;
            }
        }

        /// <summary>
        /// 过滤查询关键词
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        protected string FilterKeyword(string keyword)
        {
            foreach (var item in KeywordList)
            {
                if (keyword.Contains(item))
                    keyword = keyword.Replace(item, string.Format("{0} ", item));
            }
            return keyword;
        }

        //This happens prior to page load
        protected void Page_Init(object sender, EventArgs e)
        {
            config = new FileExplorerConfig(Server, Context);

            if (Request["lang"] != null)
            {
                //allow to pass in configuration
                config.Language = Request["lang"];
                Session["lang"] = config.Language;
            }
            else if (Session["lang"] != null)
            {
                config.Language = LoginUserBiz.LoginUserLang(); //Session["lang"].ToString();
            }
           lang = new LanguageDictionary(config.Language, Server); //load languages
            
        }
    }
}